{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] numpy.empty(shape, dtype = float, order = 'C')\n",
      "[[0 1]\n",
      " [2 3]\n",
      " [4 5]]\n",
      "\n",
      "[2] numpy.zeros(shape, dtype = float, order = 'C')\n",
      "[0. 0. 0. 0. 0.]\n",
      "\n",
      "[0 0 0 0 0]\n",
      "\n",
      "[[(0, 0.) (0, 0.)]\n",
      " [(0, 0.) (0, 0.)]]\n",
      "\n",
      "[3] numpy.ones(shape, dtype = None, order = 'C')\n",
      "[1. 1. 1. 1. 1.]\n",
      "\n",
      "numpy.asarray(a, dtype = None, order = None)\n",
      "[1 2 3]\n",
      "[1 2 3 4 5]\n",
      "\n",
      "[5] numpy.frombuffer(buffer, dtype=float, count=-1, offset=0)\n",
      "[b'H' b'e' b'l' b'l' b'o' b' ' b'W' b'o' b'r' b'l' b'd']\n",
      "\n",
      "[6] numpy.fromiter(iterable, dtype, count=-1)\n",
      "[0. 1. 2. 3. 4.]\n",
      "\n",
      "[7] numpy.arange(start, stop, step, dtype)\n",
      "[10. 12. 14. 16. 18.]\n",
      "\n",
      "[8] np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)\n",
      "[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]\n",
      "\n",
      "[9] np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)\n",
      "[1.00000000e+00 3.57200647e+00 1.27592302e+01 4.55760529e+01\n",
      " 1.62797956e+02 5.81515352e+02 2.07717660e+03 7.41968826e+03\n",
      " 2.65031745e+04 9.46695107e+04 3.38160105e+05 1.20791008e+06\n",
      " 4.31466263e+06 1.54120028e+07 5.50517738e+07 1.96645292e+08\n",
      " 7.02418257e+08 2.50904256e+09 8.96231625e+09 3.20134516e+10\n",
      " 1.14352256e+11 4.08467000e+11 1.45904677e+12 5.21172449e+12\n",
      " 1.86163136e+13 6.64975927e+13 2.37529831e+14 8.48458094e+14\n",
      " 3.03069780e+15 1.08256722e+16 3.86693710e+16 1.38127243e+17\n",
      " 4.93391407e+17 1.76239730e+18 6.29529456e+18 2.24868329e+19\n",
      " 8.03231126e+19 2.86914678e+20 1.02486109e+21 3.66081043e+21\n",
      " 1.30764386e+22 4.67091231e+22 1.66845290e+23 5.95972456e+23\n",
      " 2.12881747e+24 7.60414977e+24 2.71620722e+25 9.70230976e+25\n",
      " 3.46567132e+26 1.23794004e+27]\n"
     ]
    }
   ],
   "source": [
    "# numpy 库\n",
    "#\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "def createArray():\n",
    "    \"\"\"\n",
    "    ndarray 数组除了可以使用底层 ndarray 构造器来创建外，也可以通过以下几种方式来创建。\n",
    "    [1] numpy.empty(shape, dtype = float, order = 'C')\n",
    "        用来创建一个指定形状(shape)、数据类型(dtype)且未初始化的数组\n",
    "    [2] numpy.zeros(shape, dtype = float, order = 'C')\n",
    "        创建指定大小的数组，数组元素以 0 来填充\n",
    "    [3] numpy.ones(shape, dtype = None, order = 'C')\n",
    "        创建指定形状的数组，数组元素以 1 来填充\n",
    "    [4] numpy.asarray(a, dtype = None, order = None)\n",
    "        从已有的数组创建数组\n",
    "    [5] numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0)\n",
    "        用于实现动态数组, 接受 buffer 输入参数，以流的形式读入转化成 ndarray 对象\n",
    "    [6] numpy.fromiter(iterable, dtype, count=-1)\n",
    "        从可迭代对象中建立 ndarray 对象，返回一维数组\n",
    "    [7] numpy.arange(start, stop, step, dtype)\n",
    "        从数值范围创建数组\n",
    "    [8] np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)\n",
    "        用于创建一个一维数组，数组是一个等差数列构成\n",
    "    [9] np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)\n",
    "        创建一个于等比数列\n",
    "    \"\"\"\n",
    "\n",
    "    # [1] numpy.empty\n",
    "    print(\"[1] numpy.empty(shape, dtype = float, order = 'C')\")\n",
    "    x = np.empty([3, 2], dtype=int)\n",
    "    print(x)\n",
    "    print()\n",
    "\n",
    "    # [2] numpy.zeros\n",
    "    print(\"[2] numpy.zeros(shape, dtype = float, order = 'C')\")\n",
    "    # 默认为浮点数\n",
    "    x = np.zeros(5)\n",
    "    print(x)\n",
    "    print()\n",
    "\n",
    "    # 设置类型为整数\n",
    "    y = np.zeros((5,), dtype=int)\n",
    "    print(y)\n",
    "    print()\n",
    "\n",
    "    # 自定义类型\n",
    "    z = np.zeros((2, 2), dtype=[('x', 'i4'), ('y', 'f4')])\n",
    "    print(z)\n",
    "    print()\n",
    "\n",
    "    # [3] numpy.ones\n",
    "    print(\"[3] numpy.ones(shape, dtype = None, order = 'C')\")\n",
    "    x = np.ones(5)\n",
    "    print(x)\n",
    "    print()\n",
    "\n",
    "    # [4] numpy.asarray\n",
    "    print(\"numpy.asarray(a, dtype = None, order = None)\")\n",
    "    x = [1, 2, 3]\n",
    "    a = np.asarray(x)\n",
    "    print(a)\n",
    "    y = (1, 2, 3, 4, 5)\n",
    "    a = np.asarray(y)\n",
    "    print(a)\n",
    "    print()\n",
    "\n",
    "    # [5] numpy.frombuffer(buffer, dtype=float, count=-1, offset=0)\n",
    "    print(\"[5] numpy.frombuffer(buffer, dtype=float, count=-1, offset=0)\")\n",
    "    s = b'Hello World'\n",
    "    a = np.frombuffer(s, dtype='S1')\n",
    "    print(a)\n",
    "    print()\n",
    "\n",
    "    # [6] numpy.fromiter(iterable, dtype, count=-1)\n",
    "    print(\"[6] numpy.fromiter(iterable, dtype, count=-1)\")\n",
    "    # 使用 range 函数创建列表对象\n",
    "    list = range(5)\n",
    "    it = iter(list)\n",
    "    # 使用迭代器创建 ndarray\n",
    "    x = np.fromiter(it, dtype=float)\n",
    "    print(x)\n",
    "    print()\n",
    "\n",
    "    # [7] numpy.arange(start, stop, step, dtype)\n",
    "    print(\"[7] numpy.arange(start, stop, step, dtype)\")\n",
    "    x = np.arange(10, 20, 2, dtype='f4')\n",
    "    print(x)\n",
    "    print()\n",
    "\n",
    "    # [8] np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)\n",
    "    print(\"[8] np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)\")\n",
    "    a = np.linspace(1, 10, 10)\n",
    "    print(a)\n",
    "    print()\n",
    "\n",
    "    # [9] np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)\n",
    "    print(\"[9] np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)\")\n",
    "    # a = np.logspace(1.0,  2.0, num=10)\n",
    "    # print(a)\n",
    "    a = np.logspace(0,  90, base=2)\n",
    "    print(a)\n",
    "    return\n",
    "\n",
    "\n",
    "# go\n",
    "createArray()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.9 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "cf18841ace8313d0bc088ca146c17a6c0040e82121d5cb75c0ea07172309253d"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
